<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<ul id="categoryTree" class="ztree"></ul>
	
<script type="text/javascript">

	require([ 'jquery', 'util','dialog','zTree'], function($, util,dialog) {

		var ctxPath = "${pageContext.request.contextPath}";
		var data='${treeNodes}';
		data = util.strToObj(data);
		var zNodes = [];
		//zNodes = util.strToObj(zNodes);
		for (var i = 0; i < data.length; i++) {
			var zChildNodes = [];
			for (var j = 0; j < data[i].addressbookGroup.length; j++) {
				zChildNodes.push({
					id : "g_"+data[i].addressbookGroup[j].agId,
					pId : "c_"+data[i].addressbookGroup[j].acId,
					name : data[i].addressbookGroup[j].agName,
					open : false,
					isParent : false
				});
			}
			zNodes.push({
				id : "c_"+data[i].addressbookCategory.acId,
				pId : 0,
				name : data[i].addressbookCategory.acName,
				open : true,
				isParent : true,
				children: zChildNodes
			});
		}
		//设置参数 初始化
		var setting = {
			callback : {
				//beforeClick: zTreeBeforeClick,
				onClick : zTreeOnClick,
				//beforeExpand: beforeExpand,
				selectedMulti : false
			},
			check : {
				enable : false,
				chkStyle : 'radio',
				radioType : "level"
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			edit : {
				enable : false
			}			
		};
		var zTree = $.fn.zTree.init($("#categoryTree"), setting, zNodes);
		zTree.expandNode(zTree.getNodes()[0], true, false, false);
		//初始化完成
		
		/* //节点删除前事件处理
		function beforeRemove(treeId, treeNode) {
			var str = "该部门人员也会被删除，确定删除？";
			if(treeNode.isParent) {
				str = "该节点下还有子项，该节点和各子项人员也会被删除，仍要删除吗？";
			}
			dialog.confirm(treeNode,delCallBack,'',str);
			return false;
		};
		//删除节点回调函数
		function delCallBack(treeNode,modal){
			 $.ajax({
				   type: "POST",
				   async:false,
				   url: ctxPath + "/user/dept!delMulti.action",
				   data: "id=" + treeNode.depId,
				   success: function(msg){
					   modal.modal('hide');
					   msg = util.strToObj(msg);
					   if(msg.code == "success"){
						    zTreeOnClick();
							zTree.removeChildNodes(treeNode);
							zTree.removeNode(treeNode);
							zTree.reAsyncChildNodes(null, "refresh");
					   }
					   dialog.alert(msg.desc);
				   }
			    });
			};
			//处理是否显示删除按钮
			function showRemoveBtn(treeId, treeNode) {
				if(treeNode.parentTId==0||treeNode.parentTId==null) {
					return false;
				} else {
					return true;
				}
			};
			//鼠标移到树节点上处理的事件
			function addHoverDom(treeId, treeNode) {
				var sObj = $("#" + treeNode.tId + "_span");
				if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
				//初始化添加子节点事件
				var urladd = ctxPath + "/user/dept!input.action?id="+treeNode.depId+"&isAdd=true";
				if(treeNode.parentTId==0||treeNode.parentTId==null) {
					//--根节点
	 				urladd = ctxPath + "/user/dept!input.action?isAdd=true";
				}
				var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
					+ "' title='添加子部门' onfocus='this.blur();'></span>";
				sObj.after(addStr);
				var btnAdd= $("#addBtn_"+treeNode.tId);
				if (btnAdd) btnAdd.bind("click", function(){
					dialog.formModal(urladd,"新增子部门","400");
				});
				//初始化节点编辑事件
				var urledit = ctxPath + "/user/dept!input.action?id="+treeNode.depId+"&isAdd=false";
				var titleEdit="修改部门信息";
				if(treeNode.parentTId == 0||treeNode.parentTId == null) {
					//--根节点
	 				urledit = ctxPath + "/user/com!input.action?id="+treeNode.depId;
	 				titleEdit="修改企业信息"
				}
				var btnedit = $("#"+treeNode.tId+"_edit");
				if (btnedit) btnedit.bind("click", function(){
					dialog.formModal(urledit,titleEdit,'400');
				});
			};
			//鼠标移出节点时处理的事件
			function removeHoverDom(treeId, treeNode) {
				$("#addBtn_"+treeNode.tId).unbind().remove();
				$("#"+treeNode.tId+"_edit").unbind().remove();
			};
			//树节点点击前触发时间
			function beforeClick(treeId, treeNode) {
				$("#addBtn_"+treeNode.tId).unbind().remove();
				$("#"+treeNode.tId+"_edit").unbind().remove();
			}; */

			//点击时执行
			function zTreeOnClick(event,treeId, treeNode) {
				//var id = null;
				//if(treeNode&&!(treeNode.parentTId==0||treeNode.parentTId==null)) {
				//	id = treeNode.depId;
				//}
				//var fun =${callback};
				//fun(id);
				var clickPid = treeNode.id;
				var clickPidArrays = clickPid.split("_");
				if(clickPidArrays[0]=='c'){
					var href = ctxPath+ '/addressbook/addressbookGroup!list.action?pid='+ treeNode.id;
					//alert(url);
					util.ajaxPage(href,'',true,'div.right');
					//$('#category-tree').css("height",$("#category-list").css("height"));
				}
				if(clickPidArrays[0]=='g'){
					var href = ctxPath+ '/addressbook/addressbook!list.action?pid='+ clickPidArrays[1];
					//alert(url);
					util.ajaxPage(href,'',true,'div.right');
					//$('#category-tree').css("height",$("#category-list").css("height"));
				}				
			}

			/* //检测是否执行了拖拽
			function beforeDrag(treeId, treeNodes) {
				for (var i=0,l=treeNodes.length; i<l; i++) {
					if (treeNodes[i].drag === false) {
						return false;
					}
				}
				return true;
			}
			//drop：终止，拖拽终止前查看是否允许该拖拽
			function beforeDrop(treeId, treeNodes, targetNode, moveType) {
				var flag = targetNode ? targetNode.drop !== false : false;
				message=targetNode ?"目标节点不能为空":"目标节点不允许拖拽！";
				if(flag){
					var node = treeNodes[0];
					var isCom = false;

					if(targetNode.parentTId==0||targetNode.parentTId==null) {
						//--根节点
						isCom = true;
						if(moveType!=='inner'){
							flag = false;
							message = "不能创建企业！";
						}
					}
				}
				if(flag){
					$.ajax({
					   type: "POST",
					   async:false,
					   url: ctxPath + "/user/dept!toModify.action",
					   data: "id="+node.depId+"&depName="+node.depName+"&depId="+targetNode.depId+"&type="+moveType+"&isCom="+isCom,
					   success: function(msg){
							//--根据结果判断可否拖拽
							if(msg.trim()=='false'){
								flag = false;
							}
					   }
					});
					if(!flag){
						message = '"'+targetNode.depName+'"已存在"'+node.depName+'"子部门';
						if(moveType!=='inner'){
							message = '"'+targetNode.getParentNode().depName+'"已存在"'+node.depName+'"子部门';
						}
					}


				}
				if(!flag)dialog.alert(message);
				return flag;
			}
			function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
				
				//"inner"：成为子节点，"prev"：成为同级前一个节点，"next"：成为同级后一个节点,如果 moveType = null，表明拖拽无效
				// 如果目标和原父节点一样或和自身一样，不执行
				var node = treeNodes[0];
				var isCom = false;
				if(targetNode.parentTId==0||targetNode.parentTId==null) {
					//--根节点
					isCom = true;
				}
				$.ajax({
				   type: "POST",
				   url: ctxPath + "/user/dept!modify.action?deleteFlag=false",
				   data: "id="+node.depId+"&depId="+targetNode.depId+"&type="+moveType+"&isCom="+isCom,
				   success: function(msg){
					   dialog.alert(msg.desc);
				   }
				});
			} 

			function ztreeClick(id){
				var href = ctxPath + '/user/user!list.action';
				if(typeof id ==='string'){
					href +='?deptId='+id;
				}
				util.ajaxPage(href,'',true,'div.right');
			}
			**/
		});
	</script>

		